package com.kelvin.paopao.module.main.find.filter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.airsaid.pickerviewlibrary.OptionsPickerView;
import com.kelvin.kelvinlib.utils.LogUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.api.responseBean.TagModel;
import com.kelvin.paopao.injector.components.DaggerFindFilterComponent;
import com.kelvin.paopao.injector.components.FindFilterComponent;
import com.kelvin.paopao.injector.modules.FindFilterModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.person.edit.PersonDetailEditActivity;
import com.kelvin.paopao.module.person.tagset.ISetTagView;
import com.kelvin.paopao.utils.Constant;
import com.kelvin.paopao.widgets.CustomToolbar;
import com.kelvin.paopao.widgets.dialog.OptionSelector;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/7.
 */

public class FindFilterActivity extends BaseActivity<FindFilterPresenter> implements ISetTagView {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.iv_switch)
    ImageView ivSwitch;
    @BindView(R.id.tv_height)
    TextView tvHeight;
    @BindView(R.id.tv_weight)
    TextView tvWeight;
    @BindView(R.id.id_flowlayout)
    TagFlowLayout idFlowlayout;

    private boolean isFromFar;
    private String height, weight, sex;

    private TagAdapter tagAdapter;
    private List<String> tagDatas;
    private String user_label;

    private List<String> labelList;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_find_filter;
    }

    @Override
    protected void initInjector() {
        DaggerFindFilterComponent.builder().findFilterModule(new FindFilterModule(this))
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        sex = Constant.filterCache.getSex();
        weight = Constant.filterCache.getWeight();
        height = Constant.filterCache.getHeight();
        labelList = Constant.filterCache.getLabel();

        if (labelList != null && labelList.size() != 0) {
            StringBuilder sb = new StringBuilder();
            for (String str : labelList) {
                sb.append(str + "|");
            }
            user_label = sb.substring(0, sb.length() - 1);
        }

        if (TextUtils.isEmpty(sex) || sex.equals("")) {
            rg.check(R.id.rb1);
        } else if (sex.equals("1")) {
            rg.check(R.id.rb2);
        } else if (sex.equals("0")) {
            rg.check(R.id.rb3);
        }

        if (!TextUtils.isEmpty(weight)) {
            tvHeight.setText(weight);
        }

        if (!TextUtils.isEmpty(height)) {
            tvHeight.setText(height);
        }


        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb1) {
                    sex = "";
                } else if (checkedId == R.id.rb2) {
                    sex = "1";
                } else {
                    sex = "0";
                }
            }
        });

        customToolbar.showBackIcon(this);
        customToolbar.inflateMenu(R.menu.menu_filter);
        customToolbar.setMenuOnClick(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent();
                intent.putExtra("sex", sex);
                intent.putExtra("height", height);
                intent.putExtra("weight", weight);
                intent.putExtra("user_label", user_label);

                Constant.filterCache.setHeight(height);
                Constant.filterCache.setWeight(weight);
                Constant.filterCache.setSex(sex);
                if (!TextUtils.isEmpty(user_label)) {
                    String[] cacheLabel = user_label.split("\\|");
                    Constant.filterCache.setLabel(Arrays.asList(cacheLabel));
                }

                setResult(Activity.RESULT_OK, intent);
                finish();
                return true;
            }
        });

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mPresenter.getData(isRefresh);
    }


    @OnClick({R.id.iv_switch, R.id.ll_height, R.id.ll_weight})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_switch:
                isFromFar = !isFromFar;
                if (isFromFar) {
                    ivSwitch.setImageResource(R.drawable.icon_on_blue);
                } else {
                    ivSwitch.setImageResource(R.drawable.icon_off);
                }
                break;
            case R.id.ll_height:
                OptionSelector.showTallSelector(this).setOnOptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int option1, int option2, int option3) {
                        tvHeight.setText(OptionSelector.tallList.get(option1));
                        height = OptionSelector.tallList.get(option1);
                    }
                });
                break;
            case R.id.ll_weight:
                OptionSelector.showWeightSelector(this).setOnOptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int option1, int option2, int option3) {
                        tvWeight.setText(OptionSelector.weightList.get(option1));
                        weight = OptionSelector.weightList.get(option1);
                    }
                });
                break;
        }
    }

    @Override
    public void loadData(List<TagModel> models) {
        tagDatas = new ArrayList<>();
        for (TagModel model : models) {
            tagDatas.add(model.getLbName());
        }
        tagAdapter = new TagAdapter<String>(tagDatas) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(FindFilterActivity.this)
                        .inflate(R.layout.tag_item,
                                idFlowlayout, false);
                tv.setText(s);
                return tv;
            }
        };
        idFlowlayout.setMaxSelectCount(tagDatas.size());
        idFlowlayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {

            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                if (selectPosSet.size() != 0) {
                    user_label = getUser_label(selectPosSet);
                    LogUtil.d("user_label select:" + user_label);
                }
            }
        });
        Set<Integer> pos = new HashSet<>();
        if (labelList != null && labelList.size() != 0) {
            for (int i = 0; i < models.size(); i++) {
                if (labelList.contains(models.get(i).getLbName())) {
                    pos.add(i);
                }
            }
        }

        idFlowlayout.setAdapter(tagAdapter);
        tagAdapter.setSelectedList(pos);
    }

    @Override
    public void showNoData() {

    }

    private String getUser_label(Set<Integer> selectPosSet) {
        StringBuilder sb = new StringBuilder();
        for (Integer integer : selectPosSet) {
            sb.append(tagDatas.get(integer) + "|");
        }
        return sb.substring(0, sb.length() - 1);
    }


}
